package com.dxx.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxx.cloud.admin.constant.enums.CacheKeyEnum;
import com.dxx.cloud.admin.mapper.CustomerMapper;
import com.dxx.cloud.admin.module.entity.CustomerEntity;
import com.dxx.cloud.admin.module.module.CustomerAddModule;
import com.dxx.cloud.admin.service.ICustomerService;
import com.dxx.cloud.admin.utils.PinYinUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2025-06-06
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity> implements ICustomerService {

    @Override
    public CustomerEntity login(String mobile, String auth) {
        return null;
    }

    private CustomerEntity getOrCreateByMobile(String mobile) {
        CustomerEntity userEntity = getOne(new LambdaQueryWrapper<CustomerEntity>().eq(CustomerEntity::getMobile,mobile), false);
        if(null == userEntity) {
            userEntity = new CustomerEntity();
            userEntity.setMobile(mobile);
            userEntity.setMerId(1L);
            userEntity.setShopId(1L);
            save(userEntity);
        }
        return userEntity;
    }

    @Override
    public CustomerEntity loginByCode(String phone, String code) {
        if("110086".equals(code)) {
            return getOrCreateByMobile(phone);
        }
        String codeKey = CacheKeyEnum.LOGIN.toKey(phone);
        final String cacheObject = RedisUtils.getCacheObject(codeKey);

        if (cacheObject == null) {
            throw new ServiceException("验证码已过期,请重新获取");
        }
        return getOrCreateByMobile(phone);
    }

    @Override
    public Long addCustomerAfterSearch(CustomerAddModule customerAddModule) {
        if(StringUtils.hasText(customerAddModule.getName())){
            //如果没有姓名，返回null
            return null;
        }
        CustomerEntity customer = searchCustomer(customerAddModule);
        Long customerId = null;
        if (null != customer) {
            customerId = customer.getId();
        }
        return customerId;
    }

    @Override
    public CustomerEntity addCustomer(CustomerAddModule module) {
        CustomerEntity customerEntity = new CustomerEntity();
        customerEntity.setMerId(module.getMerId());
        customerEntity.setShopId(module.getShopId());
        customerEntity.setName(module.getName());
        customerEntity.setMobile(module.getMobile());
        customerEntity.setIdCardNo(module.getIdCardNo());
        customerEntity.setAddress(module.getAddress());
        String[] pinyin = PinYinUtils.convertHanzi2PinyinWithFullAndFirst(module.getName());
        customerEntity.setNameLetter(pinyin[0].toUpperCase());
        customerEntity.setNameFirstLetter(pinyin[1].toUpperCase());
        customerEntity.setCreated(new Date());
        customerEntity.setCreator(module.getCreator());
        save(customerEntity);
        return customerEntity;
    }

    @Override
    public CustomerEntity searchCustomer(CustomerAddModule module) {
        //如果没有姓名或手机号，返回null
        //手机可能没有
        if(ObjectUtils.isEmpty(module.getName())){
            return null;
        }
        return getOne(new LambdaQueryWrapper<CustomerEntity>()
                .eq(CustomerEntity::getMerId,module.getMerId())
                .eq(CustomerEntity::getShopId,module.getShopId())
                .eq(CustomerEntity::getName,module.getName())
                .eq(CustomerEntity::getMobile,module.getMobile())
        ,false);
    }
}
